<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        :immediate-check="false"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          :artObj="obj"
          v-for="obj in articleList"
          :key="obj.art_id"
          @click.native="toArticleDetail(obj.art_id)"
        ></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { getArticleListAPI } from '@/api'
export default {
  props: {
    channelId: Number
  },
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [],
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      nextDate: new Date()
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      /*  */
      if (this.articleList.length > 0) {
        this.getOldAddNewArticleList()
      }
    },
    async getOldAddNewArticleList () {
      const res = await getArticleListAPI({
        channel_id: this.channelId,
        timestamp: this.nextDate
        // timestamp: this.nextArticleList

      })
      // console.log(res)
      // console.log(res)
      // ?. 新语法 不适用于IE浏览器
      //
      /* if (res.data?.data?.results?.length === 0) {
        this.finished = true
      } */
      /* 将两次请求的数组放到一起，形成一个新的数组，进行页面的铺设 */
      this.articleList = [...this.articleList, ...res.data.data.results]
      /* 当 pre_timestamp 的值为 null ， 代表没有数据了，就将finished设置为 true */
      if (res.data?.data?.pre_timestamp === null) {
        this.finished = true
      } else {
        this.nextDate = res.data.data.pre_timestamp
      }
      setTimeout(() => {
        this.refreshing = false
      }, 1000)
      this.loading = false
    },
    onRefresh () {
      // 清空列表数据
      this.articleList = []
      this.nextDate = new Date()
      this.getOldAddNewArticleList()
    },
    toArticleDetail (artId) {
      this.$router.push({
        path: '/article',
        query: {
          aId: artId
        }
      })
    }
  },
  created () {
    this.getOldAddNewArticleList()
  }
}
</script>

<style>
</style>
